package com.lovo.roomProject.student.dao.impl;

import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.ClaszDO;
import com.lovo.roomProject.pojo.RoomDO;
import com.lovo.roomProject.pojo.StudentDO;
import com.lovo.roomProject.student.dao.StudenDAO;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class StudentDAOImpl extends BaseDAO implements StudenDAO {
    @Override
    public List listStudent() {
        this.getConnection();
        String sql = "select * from t_student s left join t_class c on s.s_c_id=c.c_id  left join t_room r on s.s_r_id=r.r_id";
        List list = new ArrayList();
        try {
            preparedStatement = con.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                StudentDO studentDO = new StudentDO();
                studentDO.setId(rs.getInt("s_id"));
                studentDO.setName(rs.getString("s_name"));
                studentDO.setGender(rs.getString("s_gender"));
                studentDO.setPhone(rs.getString("s_phone"));
                studentDO.setDate(LocalDate.parse(rs.getString("s_date")));
                studentDO.setHead(rs.getString("s_head"));
                /*
                班级
                 */
                ClaszDO clasz = new ClaszDO();
                clasz.setId(rs.getInt("c_id"));
                clasz.setName(rs.getString("c_name"));
                studentDO.setClasz(clasz);
                /*
                宿舍
                 */
                RoomDO room = new RoomDO();
                room.setId(rs.getInt("r_id"));
                room.setAddr(rs.getString("r_addr"));
                studentDO.setRoom(room);

                list.add(studentDO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return list;
    }
}
